﻿#region Copyrights Notice
//©2009 Microsoft Corporation.  All rights reserved.
#endregion

using System;
using System.Data;
using System.DirectoryServices;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using Microsoft.CISG.BitlockerRecovery.BL;
using Microsoft.CISG.BitlockerRecovery.CDE;
using System.Runtime.Serialization.Formatters.Binary;

namespace BitlockerRecovery
{
    public partial class AdminDefault : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        protected void Submit_OnClick(object sender, EventArgs e)
        {
            char[] StrSeperator = new char[] { '\\' };
            string AuthUser = HttpContext.Current.User.Identity.Name.ToLower(CultureInfo.CurrentCulture);

            Validate("AllValidations");
            if (Page.IsValid)
            {
                    string SrchSearchValue = SearchValue.Text.ToString();
                    string SrcRequestedFor = RequestedFor.Text.ToString().ToLower(CultureInfo.CurrentCulture);

                    //validate requested for alias
                    string[] AryRequestedFor = SrcRequestedFor.Split(StrSeperator);

                    string AliasValid = CommonBL.ActiveDirectoryLookup(AryRequestedFor[0].ToString(), "sAMAccountName=" + AryRequestedFor[1].ToString(), "manager");

                    if (AliasValid != null)
                    {

                        IdentityCDE UserInfo = new IdentityCDE();
                        UserInfo = CommonBL.GetUserADData(AuthUser);

                        DataSet ds = CommonBL.Search(SrchSearchValue, UserInfo.UserDomain + "\\" + UserInfo.UserAlias, SrcRequestedFor, 3);
                        if (ds.Tables.Count != 0)
                        {
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                try
                                {
                                    byte[] RecoveryPassword = (byte[])row["RecoveryPassword"];
                                    row["RecoveryPasswordString"] = Encryption.DecryptBitlocker(RecoveryPassword);
                                }
                                catch (CryptographicException ex)
                                {
                                    LabelMessage.Text = "An error has occurred.  It has been logged for follow-up by the system administrator.";
                                    LogErrorsBL.InsertErrorLog(Request.Path.ToString(), Request.RawUrl + ": " + ex.Message);
                                }
                            }
                            Results.DataSource = ds;
                            Results.DataBind();
                            SearchMultiview.SetActiveView(ResultsView);
                            LabelMessage.Text = "";
                        }
                        else
                        {
                            LabelMessage.Text = "No records found";
                            SearchMultiview.SetActiveView(ViewSearch);
                        }
                        FaqLink.Text = "<a href='" + ResolveClientUrl("~/Faq.aspx") + "'>Faq</a>";

                    }
                    else
                    {
                        LabelMessage.Text = "Invalid domain\\alias was entered.";
                        SearchMultiview.SetActiveView(ViewSearch);
                    }
            }
            else
            {
                LabelMessage.Text = "An error has occurred.  It has been logged for follow-up by the system administrator.";
                LogErrorsBL.InsertErrorLog(Request.Path.ToString(), Request.RawUrl + ": Clientside validation was bypassed by: " + AuthUser);

            }

        }
        protected void Back_OnClick(object sender, EventArgs e)
        {
            SearchMultiview.SetActiveView(ViewSearch);
        }

        
    }
}
